package logic

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/astaxie/beego/orm"
	"net/url"
)

func AddStudent(input url.Values) *utils.Error {
	if len(input["school_id"][0]) <= 0 {
		return utils.NewError(0, "请选择学校")
	}
	if len(input["class_id"][0]) <= 0 {
		return utils.NewError(0, "请选择班级")
	}
	if len(input["sn"][0]) <= 0 {
		return utils.NewError(0, "请输入唯一标识(卡号)")
	}
	if len(input["real_name"][0]) <= 0 {
		return utils.NewError(0, "请输入姓名")
	}
	if len(input["id_card"][0]) <= 0 {
		return utils.NewError(0, "请输入身份证号")
	}

	var p map[string]interface{}

	/*p = map[string]interface{}{}
	p["id"] = input["school_id"][0]
	school := service.GetSchoolInfo(p)

	p = map[string]interface{}{}
	p["num"] = input["sn"][0]
	p["relation"] = true
	cardNumLib := service.GetCardNumLibInfo(p)
	if cardNumLib.Id <= 0 {
		return utils.NewError(0, fmt.Sprintf("%s是非法卡号，不在卡号库中", input["sn"][0]))
	}

	schoolId := utils.Atoi64(input["school_id"][0])
	if cardNumLib.School.Id != schoolId {
		return utils.NewError(0, fmt.Sprintf("%s是非法卡号，不属于%s", input["sn"][0], school.Name))
	}*/

	p = map[string]interface{}{}
	p["idCard"] = input["id_card"][0]
	studentInfo := service.GetStudentInfo(p)
	if studentInfo.Id > 0 {
		return utils.NewError(0, fmt.Sprintf("身份证号%s已被%s同学使用，请更换其他身份证号", input["id_card"][0], studentInfo.RealName))
	}

	p = map[string]interface{}{}
	p["sn"] = input["sn"][0]
	studentInfo = service.GetStudentInfo(p)
	if studentInfo.Id > 0 {
		return utils.NewError(0, fmt.Sprintf("卡号%s已被%s同学使用，请更换其他卡号", input["sn"][0], studentInfo.RealName))
	}

	_, err := service.AddStudent(input)
	if err != nil {
		return utils.NewError(0, "添加失败")
	}

	return nil
}

func AddStudentFromExcel(input url.Values) *utils.Error {
	if len(input["class_id"][0]) <= 0 {
		return utils.NewError(0, "请选择班级")
	}
	if len(input["excel_file_path"][0]) <= 0 {
		return utils.NewError(0, "请上传excel文件")
	}

	f, err := excelize.OpenFile("." + input["excel_file_path"][0])
	if err != nil {
		return utils.NewError(0, "excel文件无法打开，请重新上传")
	}

	o := orm.NewOrm()

	school := &models.School{}
	if _, ok := input["school_id"]; ok {
		school.Id = utils.Atoi64(input["school_id"][0])
		o.Read(school)
	}

	class := &models.Class{}
	if _, ok := input["class_id"]; ok {
		class.Id = utils.Atoi64(input["class_id"][0])
		o.Read(class)
	}

	var p map[string]interface{}
	var students []models.Student
	sex := map[string]int{
		"男": 0,
		"女": 1,
	}

	rows := f.GetRows("Sheet1")
	for k, row := range rows {
		if k == 0 {
			continue
		}

		/*p = map[string]interface{}{}
		p["num"] = row[1]
		p["relation"] = true
		cardNumLib := service.GetCardNumLibInfo(p)
		if cardNumLib.Id <= 0 {
			return utils.NewError(0, fmt.Sprintf("%s是非法卡号，不在卡号库中", row[1]))
		}

		if cardNumLib.School.Id != school.Id {
			return utils.NewError(0, fmt.Sprintf("%s是非法卡号，不属于%s", row[1], school.Name))
		}*/

		p = map[string]interface{}{}
		p["idCard"] = row[1]
		studentInfo := service.GetStudentInfo(p)
		if studentInfo.Id > 0 {
			return utils.NewError(0, fmt.Sprintf("身份证号%s已被%s同学使用，请更换其他身份证号", row[1], studentInfo.RealName))
		}

		p = map[string]interface{}{}
		p["sn"] = row[3]
		studentInfo = service.GetStudentInfo(p)
		if studentInfo.Id > 0 {
			return utils.NewError(0, fmt.Sprintf("卡号%s已被%s同学使用，请更换其他卡号", row[3], studentInfo.RealName))
		}

		student := models.Student{}
		student.School = school
		student.Class = class
		student.RealName = row[0]
		student.IdCard = row[1]
		student.Sex = sex[row[2]]
		student.Sn = row[3]
		student.Status = 1
		student.TempIsNormal = 1
		students = append(students, student)
	}

	_, err = o.InsertMulti(1, students)
	if err != nil {
		return utils.NewError(0, "导入失败")
	}

	return nil
}

func EditStudent(input url.Values) *utils.Error {
	if len(input["school_id"][0]) <= 0 {
		return utils.NewError(0, "请选择学校")
	}
	if len(input["class_id"][0]) <= 0 {
		return utils.NewError(0, "请选择班级")
	}
	if len(input["sn"][0]) <= 0 {
		return utils.NewError(0, "请输入唯一标识(卡号)")
	}
	if len(input["real_name"][0]) <= 0 {
		return utils.NewError(0, "请输入姓名")
	}
	if len(input["id_card"][0]) <= 0 {
		return utils.NewError(0, "请输入身份证号")
	}

	var p map[string]interface{}

	/*p = map[string]interface{}{}
	p["id"] = input["school_id"][0]
	school := service.GetSchoolInfo(p)

	p = map[string]interface{}{}
	p["num"] = input["sn"][0]
	p["relation"] = true
	cardNumLib := service.GetCardNumLibInfo(p)
	if cardNumLib.Id <= 0 {
		return utils.NewError(0, fmt.Sprintf("%s是非法卡号，不在卡号库中", input["sn"][0]))
	}

	schoolId := utils.Atoi64(input["school_id"][0])
	if cardNumLib.School.Id != schoolId {
		return utils.NewError(0, fmt.Sprintf("%s是非法卡号，不属于%s", input["sn"][0], school.Name))
	}*/

	p = map[string]interface{}{}
	p["notId"] = input["id"][0]
	p["idCard"] = input["id_card"][0]
	studentInfo := service.GetStudentInfo(p)
	if studentInfo.Id > 0 {
		return utils.NewError(0, fmt.Sprintf("身份证号%s已被%s同学使用，请更换其他身份证号", input["id_card"][0], studentInfo.RealName))
	}

	p = map[string]interface{}{}
	p["notId"] = input["id"][0]
	p["sn"] = input["sn"][0]
	studentInfo = service.GetStudentInfo(p)
	if studentInfo.Id > 0 {
		return utils.NewError(0, fmt.Sprintf("卡号%s已被%s同学使用，请更换其他卡号", input["sn"][0], studentInfo.RealName))
	}

	_, err := service.EditStudent(input)
	if err != nil {
		return utils.NewError(0, "修改失败")
	}

	return nil
}
